package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class OrderDaoImpl extends BaseDao implements IOrderDao {
    @Override
    public CutPageBean<OrderBean> cutAll(int pageNO) {
        CutPageBean<OrderBean> cutBean = new CutPageBean<>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_order limit ?,?");
            ps.setObject(1,(pageNO - 1)*CutPageBean.PAGESIZE);
            ps.setObject(2,CutPageBean.PAGESIZE);

            rs = ps.executeQuery();
            while (rs.next()){
                OrderBean order = this.getOrder();
                cutBean.getList().add(order);
            }

            ps = con.prepareStatement("select count(*) c from t_order");
            rs = ps.executeQuery();
            if(rs.next()){
                cutBean.setCount(rs.getInt("c"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }

        return cutBean;
    }

    private OrderBean getOrder() throws SQLException {
        OrderBean order = new OrderBean();
        order.setId(rs.getInt("pk_orderId"));
        order.setUserName(rs.getString("o_name"));
        order.setOrderDate(LocalDate.parse(rs.getString("o_date")));
        order.setAddress(rs.getString("o_address"));
        return order;
    }

    @Override
    public void add(OrderBean order) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_order(o_name,o_date,o_address) values(?,?,?)");
            ps.setObject(1,order.getUserName());
            ps.setObject(2,order.getOrderDate());
            ps.setObject(3,order.getAddress());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_order where pk_orderId=?");
            ps.setObject(1,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_order set o_address=? where pk_orderId=?");
            ps.setObject(1,address);
            ps.setObject(2,id);

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public OrderBean findById(int id) {
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_order where pk_orderId=?");
            ps.setObject(1,id);

            rs = ps.executeQuery();
            while (rs.next()){
                return this.getOrder();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
        return null;
    }
}
